package com.literature.threeGroup.controller;


import com.literature.threeGroup.pojo.Order;
import com.literature.threeGroup.service.OrderService;
import com.literature.threeGroup.util.ExcelUtil;
import com.literature.threeGroup.util.SetResponseHeaderUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Order")
@CrossOrigin   //跨域i
public class OrderController {

    @Autowired
    private OrderService service;



    @RequestMapping(value = "/findAll")
    public List<Order> FindAll(){
        SimpleDateFormat sjgs=new SimpleDateFormat("yyyy");
        Date a=new Date();
        System.out.println("当前年份"+sjgs.format(a));
        String format = sjgs.format(a);

        return  service.FindAll(format);
    }
    /**
     * 年度列表显示
     * @param year  非空查出年度列表，空置
     * @return
     */
    @RequestMapping(value = "/findAll/{year}")
    public List<Order> FindYear(@PathVariable String year){
        System.out.println("年限:"+year);
       return  service.FindAll(year+"");
    }


    //续停顶

    /**
     * 续停顶列表
     * @param year  最新年限:最新年限所包含的东西我不要
     * @param type  期刊/非刊
     * @return
     */
    @RequestMapping(value="xtding/{year}/{type}")
    public List<Order> xtdingAll(@PathVariable String year,@PathVariable String type){
        System.out.println(year+","+type);
        return service.xtdingAll(year,type);
    }

    //类型回显

    /**
     * 续停订类型下拉回显
     * @return
     */
    @RequestMapping(value="/getType")
    public List<Order> getTypes(){
        System.out.println("下拉");
        return service.getTypes();
    }

    //order_state审核状态,点击续订=0 停订=1

    /**
     *      续订退订
     * @param state   order_state
     * @param id        进入待审核的数据的id
     *                 所有被停订的数据都会给一个停订的时间
     * @return
     */
    @RequestMapping("updateState/{id}/{state}")
    public void updateState(@PathVariable String state,@PathVariable String id){
        System.out.println(id+",state:"+state);
        if(state.equals("0")){
            state="停订";
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            Date date=new Date();
            service.updateDiscontinueData(simpleDateFormat.format(date),id);
            service.updateState(state,id);
        }else if(state.equals("1")){
            state="续订";
            //service.updateDiscontinueData(state,id);
            service.updateState(state,id);
        }

    }


    /**
     * 审核订单
     * @return
     */
    @RequestMapping("/auditList")
    public List<Order> auditList(){
        return service.auditList();
    }




    /**
     *
     * @param review 审核确认 0通过，1不通过
     * @param id 要通过的ID
     */
    @RequestMapping("updateOrder/{review}/{id}")
    public void updateOrder(@PathVariable String review,@PathVariable String id){
        System.out.println(review+","+id);
        if("1".equals(review)){
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy");
            Date date=new Date();
            //查询这个ID的单条数据
            Order order=service.selectById(id);
            order.setOrderYear(simpleDateFormat.format(date));
            service.insertOrder(order);
        }else if("0".equals(review)){
            System.out.println("审核驳回");
            //查询这个ID的单条数据
            service.updateOrder(review,id);
        }
    }

    /**
     * 年度订单年限回显
     * @param year  年限选择
     * @return
     */
    @RequestMapping("/selectYear")
    public List<Order> selectYear(String year){
        return service.selectYear();
    }
    //

    /**
     * 获取停订数据
     * @return
     */
    public List<Order> getDiscontinue(String Date){
        return service.getDiscontinue();
    }

    /**
     * 获取三年未到货的
     * order_year  订购年份列
     * is_arrival  是否到货列
     * deliverytime 到货时间
     * is_arrival 是否到货
     * create_date 创建时间
     */
    @RequestMapping("/time")
    public List<Order> getNotArrivedForThreeYears(){
        //获得当前时间\
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy");
        Date a=new Date();
        System.out.println(simpleDateFormat.format(a));
        //获取创建订单时间
        String format = simpleDateFormat.format(a);
        int i = Integer.parseInt(format);
        //查询3年未到货的(未到货并且创建时间小于当前时间3年的)
        return service.getNotArrivedForThreeYears(i);

    }

    /**
     * 导出数据..
     * @param response
     * @throws Exception
     */
    @RequestMapping("ExportExcel")
    public void ExportExcel(HttpServletResponse response) throws Exception {
        SimpleDateFormat sjgs=new SimpleDateFormat("yyyy");
        Date aa=new Date();
        System.out.println("当前年份"+sjgs.format(aa));
        String format = sjgs.format(aa);
        //拿到数据
        List<Order> allLog = service.FindAll(format);

        for (Order a:allLog) {
            System.err.println(a.toString());
        }
        //设置excel标题
        String[] title={"ID","题名","订购机构","订购年","支付金额","数据库支持","载体","创建日期","修改日期"};

        String fileName="移送单导出.xls";

        String sheetName="移送单表";

        //决定 行 列  1代表多少行 2 代表多少列
        String [][] content = new String[allLog.size()][5];
        for (int i = 0; i < allLog.size(); i++) {
            content[i] = new String[title.length];
            Order obj = allLog.get(i);
            content[i][0] = obj.getId();
            content[i][1] = obj.getTitle();
            content[i][2] = obj.getOrderOrganization();
            content[i][3] = obj.getOrderYear();
            content[i][4] = obj.getPay().toString();
            content[i][5] = obj.getDatabaseSupport();
            content[i][6] = obj.getCarrier();
            content[i][7] = obj.getCreateDate().toString();
            content[i][7] = obj.getUpdateDate().toString();
        }

        //创建HSSFWorkbook    名称 标题  内容 下标 !
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName,title,content);
        //响应到客户端
        try {
            SetResponseHeaderUtil.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            //将表格写入
            wb.write(os);
            //释放资源
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
